<?php
if ( !defined('IN_SEC') ){exit();}
?>
<?php include template("admin_header");?>
<div class="main-content">
					<div class="breadcrumbs" id="breadcrumbs">
						<script type="text/javascript">
							try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
						</script>

<?php include template("admin_breadcrumb");?>
						<div class="nav-search" id="nav-search">
							<form class="form-search">
								<span class="input-icon">
									<input type="text" placeholder="Search ..." class="nav-search-input" id="nav-search-input" autocomplete="off" />
									<i class="icon-search nav-search-icon"></i>
								</span>
							</form>
						</div><!-- #nav-search -->
					</div>

					<div class="page-content">
						<div class="page-header">
							<h1><?php echo $_G['admincp']['actname']; ?><small><i class="icon-double-angle-right"></i><?php echo $_G['admincp']['optname']; ?></small></h1>
						</div><!-- /.page-header -->

						<div class="row">
							<div class="col-xs-12">
								<!-- PAGE CONTENT BEGINS -->

								<div class="row-fluid">
									<div class="span12">
										<div class="widget-box">
											<div class="widget-header widget-header-blue widget-header-flat">
												<h4 class="lighter">新成员</h4>
											</div>

											<div class="widget-body">
												<div class="widget-main">
													<div id="fuelux-wizard" class="row-fluid" data-target="#step-container">
														<ul class="wizard-steps">
															<li data-target="#step1" class="active">
																<span class="step">1</span>
																<span class="title">填写信息</span>
															</li>

															<li data-target="#step2">
																<span class="step">2</span>
																<span class="title">验证权限</span>
															</li>
														</ul>
													</div>

													<hr />
													<div class="step-content row-fluid position-relative" id="step-container">
													<form class="form-horizontal" action="?action=account&operation=edit&id=<?php echo $admin['id']; ?>" id="validation-form" method="post">
														<div class="step-pane active" id="step1">
																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="username">HeivrID:</label>

																	<div class="col-xs-12 col-sm-9">
																		<div class="clearfix">
																			<input type="text" id="username" name="username" value="<?php echo $admin['username']; ?>" <?php echo $admin ? 'readonly="readonly"' : ''; ?> class="col-xs-12 col-sm-5" />
																		</div>
																	</div>
																</div>

																<div class="space-2"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="password">密码:</label>

																	<div class="col-xs-12 col-sm-9">
																		<div class="clearfix">
																			<input type="password" name="password" id="password" class="col-xs-12 col-sm-4" />
																		</div>
																	</div>
																</div>

																<div class="space-2"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="password2">确认密码:</label>

																	<div class="col-xs-12 col-sm-9">
																		<div class="clearfix">
																			<input type="password" name="password2" id="password2" class="col-xs-12 col-sm-4" />
																		</div>
																	</div>
																</div>

																<div class="hr hr-dotted"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="realname">姓名:</label>

																	<div class="col-xs-12 col-sm-9">
																		<div class="clearfix">
																			<input type="text" id="realname" value="<?php echo $admin['realname']; ?>" name="realname" class="col-xs-12 col-sm-5" />
																		</div>
																	</div>
																</div>

																<div class="space-2"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="phone">联系电话:</label>

																	<div class="col-xs-12 col-sm-9">
																		<div class="input-group">
																			<span class="input-group-addon">
																				<i class="icon-phone"></i>
																			</span>

																			<input type="tel" id="phone" value="<?php echo $admin['mobile']; ?>" maxlength="11" name="phone" />
																		</div>
																	</div>
																</div>

																<div class="space-2"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right">性别</label>

																	<div class="col-xs-12 col-sm-9">
																		<div>
																			<label class="blue">
																				<input name="gender" value="1" <?php echo $admin['gender'] ? 'checked' : ''; ?> type="radio" class="ace" />
																				<span class="lbl"> 男</span>
																			</label>
																		</div>

																		<div>
																			<label class="blue">
																				<input name="gender" value="0" <?php echo !$admin['gender'] ? 'checked' : ''; ?> type="radio" class="ace" />
																				<span class="lbl"> 女</span>
																			</label>
																		</div>
																	</div>
																</div>

																<div class="hr hr-dotted"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="state">所属公司</label>

																	<div class="col-xs-12 col-sm-9">
																		<select id="companyid" name="companyid" class="select2" <?php echo $admin['id'] == 1 ? 'disabled="disabled"' : ''; ?> data-placeholder="选择..."><?php echo option($companys, $admin['companyid']); ?></select>
																	</div>
																</div>

																<div class="space-2"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="state">部门</label>
																	<div class="col-xs-12 col-sm-9">
																		<select id="departmentid" name="departmentid" class="select2" <?php echo $admin['id'] == 1 ? 'disabled="disabled"' : ''; ?> data-placeholder="选择..."><?php echo option($departments, $admin['departmentid']); ?></select>
																	</div>
																</div>

																<div class="space-2"></div>

																<div class="form-group">
																	<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="state">职位</label>
																	<div class="col-xs-12 col-sm-9">
																		<select id="groupid" name="groupid" class="select2" <?php echo $admin['id'] == 1 ? 'disabled="disabled"' : ''; ?> data-placeholder="选择..."><?php echo option($groups, $admin['groupid'], '无职位'); ?></select>
																	</div>
																</div>
														</div>

														<div class="step-pane" id="step2">
<?php include template("admin_uac");?>
														</div>
														</form>
													</div>

													<hr />
													<div class="row-fluid wizard-actions">
														<button class="btn btn-prev"><i class="icon-arrow-left"></i>上一步</button>
														<button class="btn btn-success btn-next" data-last="完成">下一步<i class="icon-arrow-right icon-on-right"></i></button>
													</div>
												</div><!-- /widget-main -->
											</div><!-- /widget-body -->
										</div>
									</div>
								</div>
								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					</div><!-- /.page-content -->
				</div>
<?php include template("admin_footer");?>
		<!-- page specific plugin styles -->

		<link rel="stylesheet" href="/static/ace/assets/css/select2.css" />
		<!-- page specific plugin scripts -->

		<script src="/static/ace/assets/js/fuelux/fuelux.wizard.min.js"></script>
		<script src="/static/ace/assets/js/jquery.validate.min.js"></script>
		<script src="/static/ace/assets/js/additional-methods.min.js"></script>
		<script src="/static/ace/assets/js/bootbox.min.js"></script>
		<script src="/static/ace/assets/js/jquery.maskedinput.min.js"></script>
		<script src="/static/ace/assets/js/select2.min.js"></script>
		<script type="text/javascript">
			jQuery(function($) {
			
				$('[data-rel=tooltip]').tooltip();
			
				$(".select2").css('width','200px').select2({allowClear:true})
				.on('change', function(){
					$(this).closest('form').validate().element($(this));
				}); 
			
			
				var $validation = true;
				$('#fuelux-wizard').ace_wizard().on('change' , function(e, info){
					if(info.step == 1 && $validation) {
						if( !$('#validation-form').valid() ){
							return false;
						}
					}
				}).on('finished', function(e) {
					var data = $('#validation-form').serialize();
					var loading = bootbox.dialog({
						message: '<div class="tree-loading" style="text-align: center;"><i class="icon-refresh icon-spin blue"></i></div>' 
					});
					D.ajax.postdata($('#validation-form').attr('action'), data, function(json){
						bootbox.hideAll();
						var buttons = json.error ? {
							danger : {
								label : "错误",
								className : "btn-sm btn-primary"
							}
						} : {
							success : {
								label : "OK",
								className : "btn-sm btn-primary",
								callback : function(){
									window.location.reload();
								}
							}
						};
						bootbox.dialog({
							message: json.data, 
							buttons: buttons
						});
					});
				}).on('stepclick', function(e){
					//return false;//prevent clicking on steps
				});

				//documentation : http://docs.jquery.com/Plugins/Validation/validate
			
				$.mask.definitions['~']='[+-]';
				$('#phone').mask('99999999999');
			
				jQuery.validator.addMethod("phone", function (value, element) {
					return this.optional(element) || /^1[3458][\d]{9}$/.test(value);
				}, "请输入一个有效的手机号码.");
			
				$('#validation-form').validate({
					errorElement: 'div',
					errorClass: 'help-block',
					focusInvalid: false,
					rules: {
						email: {
							required: true
						},
<?php if(!$admin){?>
						password: {
							required: true,
							minlength: 6
						},
						password2: {
							required: true,
							minlength: 6,
							equalTo: "#password"
						},
<?php } else { ?>
						password: {
							required: false,
							minlength: 6
						},
						password2: {
							required: false,
							minlength: 6,
							equalTo: "#password"
						},
<?php }?>
						username : {
							required : true
						},
						realname : {
							required: true
						},
						phone: {
							required: true,
							phone: 'required'
						},
						url: {
							required: true,
							url: true
						},
						departmentid : {
							required: true
						},
						groupid: {
							required: true
						},
						state: {
							required: true
						},
						platform: {
							required: true
						},
						subscription: {
							required: true
						},
						gender: 'required',
						agree: 'required'
					},
			
					messages: {
						departmentid : {
							required : '您必须为此成员选择一个部门，如果该公司内没有此成员所适配的部门，请联系管理员添加'
						},
						groupid : {
							required : '您必须为此成员选择一个职位，如果该部门下没有此成员所适配的职位，请联系管理员添加'
						},
						username : {
							required : '请输入HeivrID'
						},
						realname : {
							required: "请输入姓名"
						},
						password: {
							required: "请输入密码.",
							minlength: "密码长度最低6位."
						},
						subscription: "Please choose at least one option",
						gender: "请选择性别",
						agree: "Please accept our policy"
					},
			
					invalidHandler: function (event, validator) { //display error alert on form submit   
						$('.alert-danger', $('.login-form')).show();
					},
			
					highlight: function (e) {
						$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
					},
			
					success: function (e) {
						$(e).closest('.form-group').removeClass('has-error').addClass('has-info');
						$(e).remove();
					},
			
					errorPlacement: function (error, element) {
						if(element.is(':checkbox') || element.is(':radio')) {
							var controls = element.closest('div[class*="col-"]');
							if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
							else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
						}
						else if(element.is('.select2')) {
							error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
						}
						else if(element.is('.chosen-select')) {
							error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
						}
						else error.insertAfter(element.parent());
					},
			
					submitHandler: function (form) {
					},
					invalidHandler: function (form) {
					}
				});
			
				$('#modal-wizard .modal-header').ace_wizard();
				$('#modal-wizard .wizard-actions .btn[data-dismiss=modal]').removeAttr('disabled');
			});
			D.Main = {
				init : function(){
					$('#companyid').change(function(){
						var companyid = $(this).val();
						var html = '';
						$('#departmentid').html(html + '<option disabled="disabled">数据加载中...</optgroup>').select2({allowClear:true}).change();
						D.ajax.get('?action=account&operation=edit&companyid='+companyid+'&act=get_departments', function(json){
							if( json.data ){
								$.each(json.data, function(i, n){
									html += '<option value="'+n.id+'">'+n.name+'</option>';
								});
							}
							$('#departmentid').html(html).select2({allowClear:true}).change();
						});
					});
					$('#departmentid').change(function(){
						var departmentid = $(this).val();
						var html = '<option>无职位</option>';
						$('#groupid').html(html + '<option disabled="disabled">数据加载中...</optgroup>').select2({allowClear:true});
						D.ajax.get('?action=account&operation=edit&departmentid='+departmentid+'&act=get_groups', function(json){
							if( json.data ){
								$.each(json.data, function(i, n){
									html += '<option value="'+n.id+'">'+n.name+'</option>';
								});
							}
							$('#groupid').html(html).select2({allowClear:true});
						});
					});
				}
			}
		</script>
</body>
</html>
